import SDKBase from "./SDKBase";
import { FileEncoding, SDKState } from "../SDKDefine";

export default class SDKFileSystem extends SDKBase {

    protected fs: any;

    protected data_path: string;

    constructor() {
        super();
        this.fs = this.sdk.getFileSystemManager();
        this.data_path = this.sdk.getDataPath()
    }
    writeFile(fileName: string, data: any, encoding: string, callback: (r: number) => void) {
        let filePath = this.data_path + '/' + fileName;
        console.log('writeFile filePath ', filePath, encoding)
        console.log('data ', data)
        this.fs.writeFile({
            filePath: filePath,  //写入的文件路径
            data: data,  //写入的文本
            encoding: encoding,  //字符编码格式
            success: function () { //成功回调
                console.log(' writeFile ok')
                callback(SDKState.SUCCESS)
            },
            fail: function (err: any) {  //失败回调
                console.log(' writeFile fail ', err)
                callback(SDKState.FAIL)
            },
            complete: function () {  //完成回调
                console.log(' writeFile complete')
            }
        })
    }

    readFile(fileName: string, encoding: string, callback: (r: string) => void) {
        let filePath = this.data_path + '/' + fileName;
        console.log('readFile filePath ', filePath, encoding)
        this.fs.readFile({
            filePath: filePath,  //写入的文件路径
            encoding: encoding,  //字符编码格式
            success: function (res: any) { //成功回调
                console.log(' readFile ok')
                console.log('res.data ', res)
                console.log(' type is ', typeof res.data)
                if (encoding == FileEncoding.BINARY) {
                    // let bytes: Uint8Array = new Uint8Array(res.data)
                    // console.log(' res.data.length ', res.data.length)
                    var buffer = new ArrayBuffer(res.data.length);
                    var dataview = new DataView(buffer);

                    for (var i = 0; i < res.data.length; i++) {
                        dataview.setUint8(i, parseInt(res.data[i]))
                    }

                    // var ints = new Uint8Array(res.data);
                    // callback(ResultState.YES, dataview.buffer)
                } else {


                }
                callback(res.data)
            },
            fail: function (err: any) {  //失败回调
                console.log(' readFile fail', err)
                callback(null)
            },
            complete: function () {  //完成回调
                console.log(' writeFile complete')
            }
        })
    }

    writeFileSync(fileName: string, data: string | ArrayBuffer, encoding: string) {
        let filePath = this.data_path + '/' + fileName;
        console.log('writeFileSync filePath ', filePath, encoding)
        let result = this.fs.writeFileSync(filePath, data, encoding)
        return result
    }

    readFileSync(fileName: string, encoding: string) {
        let filePath = this.data_path + '/' + fileName;
        console.log('readFileSync filePath ', filePath, encoding)
        try {
            let res = this.fs.readFileSync(filePath, encoding)
            // console.log(' readFileSync ok', res)
            if (encoding == FileEncoding.BINARY) {
                var ints = new Uint8Array(res);
                return ints
            } else {
                return res;
            }

        } catch (error) {
            // console.log(' error ', error)
            return null;
        }

    }
}
